# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=kicad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-meta"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-footprints"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-symbols"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-templates"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D")
pkgver=5.1.9
pkgrel=1
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=("any")
url="http://www.kicad-pcb.org"
license=("GPLv3+")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-glm"
         "${MINGW_PACKAGE_PREFIX}-ngspice"
         "${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-wxPython"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-oce")
source=("${_realname}-${pkgver}.tar.gz"::"https://gitlab.com/kicad/code/kicad/-/archive/${pkgver}/kicad-${pkgver}.tar.gz"
        "${_realname}-i18n-${pkgver}.tar.gz"::"https://gitlab.com/kicad/code/kicad-i18n/-/archive/${pkgver}/kicad-i18n-${pkgver}.tar.gz"
        "${_realname}-footprints-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-footprints/-/archive/${pkgver}/kicad-footprints-${pkgver}.tar.gz"
        "${_realname}-symbols-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-symbols/-/archive/${pkgver}/kicad-symbols-${pkgver}.tar.gz"
        "${_realname}-templates-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-templates/-/archive/${pkgver}/kicad-templates-${pkgver}.tar.gz"
        "${_realname}-packages3D-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-packages3D/-/archive/${pkgver}/kicad-packages3D-${pkgver}.tar.gz"
        '0001-fix-FindwxWidgets.cmake.patch'
        '0002-fix-callq-0x63.patch')
sha256sums=('841be864b9dc5c761193c3ee9cbdbed6729952d7b38451aa8e1977bdfdb6081b'
            'f2cadcae1400bf483668ceccd190dff3d9dfd098307c146d273ed0bb01f4882f'
            '415e014364d1c12584f115a4adfeec1b71e41e2cd7f4ae543237ee71b8ef41bd'
            'cdb033cc755cc66a087b44fff1d2b77bf2dd44311a02c81a516b8ca1fbd242a7'
            '0c1bf3d2e6d8d1056a5da6c1f7a173551c154b4bdaddb86b6a34155b18e65da6'
            'efce7b0b65ece76b57002cd4b84152fb359f7c47fe0b3d71166e9255ffa5c5b0'
            '3fe6d1126dc3a4e3d720714fc08ca8d10771f3d38f2a2118b95f9ca41ace0c2d'
            'c5a0e1ae7ca83c5e916e9895259b24e74524ff0f9474ff7c414e968cb0e9dfe2')

_doc=("ca" "de" "en" "es" "fr" "id" "it" "ja" "nl" "pl" "ru" "zh")
 _sub=("footprints" "symbols" "templates" "packages3D")

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-fix-FindwxWidgets.cmake.patch
  patch -p1 -i "${srcdir}"/0002-fix-callq-0x63.patch
}

build() {
  cd "${srcdir}"
  msg2 "Build kicad"
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DCMAKE_PREFIX_PATH=;DEFAULT_INSTALL_PATH=" \
  cmake \
    -G"MinGW Makefiles" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DDEFAULT_INSTALL_PATH=${MINGW_PREFIX} \
    -DOPENSSL_ROOT_DIR=${MINGW_PREFIX} \
    -DKICAD_SCRIPTING=ON \
    -DKICAD_SCRIPTING_MODULES=ON \
    -DKICAD_SCRIPTING_WXPYTHON=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python2.exe \
    ../${_realname}-${pkgver}

  mingw32-make

  cd "${srcdir}"
  msg2 "Build translations"
  [[ -d build-i18n ]] && rm -rf build-i18n
  mkdir -p build-i18n && cd build-i18n

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G "MinGW Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-i18n-${pkgver}

  for _s in ${_sub[@]}; do
    cd ${srcdir}
    msg2 "Build ${_s}"
    [[ -d build-${_s} ]] && rm -rf build-${_s}
    mkdir -p build-${_s} && cd build-${_s}

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -G "MinGW Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${_s}-${pkgver}
  done
}

for _doclang in ${_doc[@]}; do _doc_pkg+=("\"${MINGW_PACKAGE_PREFIX}-${_realname}-doc-${_doclang}: Documentation for KiCad (mingw-w64)\""); done
for _feature in ${_sub[@]}; do _sub_pkg+=("\"${MINGW_PACKAGE_PREFIX}-${_realname}-${_feature}\""); done

eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}() {

  msg2 \"Package kicad\"
  optdepends+=(${_doc_pkg[@]} ${_sub_pkg[@]})
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)

  cd \${srcdir}/build-${MINGW_CHOST}
  mingw32-make DESTDIR=\${pkgdir} install

  cd \${srcdir}/build-i18n
  mingw32-make DESTDIR=\${pkgdir} install
}"

eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-meta() {
  depends=(${MINGW_PACKAGE_PREFIX}-${_realname}
           ${_sub_pkg[@]})
}"

for _part in ${_sub[@]}; do
  eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}() {
    options=('!strip')
    conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}-git)
    replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}-git)
    msg2 \"Package ${_part}\"
    cd \${srcdir}/build-${_part}
    mingw32-make DESTDIR=\${pkgdir} install
  }"
done
